package com.wlps.service.impl;

import com.wlps.config.RedisConfig;
import com.wlps.contant.Contant;
import com.wlps.entity.SysPermission;
import com.wlps.mapper.SysPermissionMapper;
import com.wlps.service.ISysPermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 权限表（菜单表）（sys_permission） 服务实现类
 * </p>
 *
 * @author ZWYZY
 * @since 2020-07-30
 */
@Service
@Transactional
public class SysPermissionServiceImpl extends ServiceImpl<SysPermissionMapper, SysPermission> implements ISysPermissionService {
    @Autowired
    RedisTemplate redisTemplate;
    @Override
// @Cacheable(value = "permissions",key = "#userId")
    public List<SysPermission> selectPermissionByUserId(Long userId) {
//        if(redisTemplate.hasKey(Contant.PERMISSIONS_KEY))
//        {
//            return (List<SysPermission>)redisTemplate.opsForValue().get(Contant.PERMISSIONS_KEY+userId);
//        }
        List<SysPermission> sysPermissionList=this.baseMapper.selectPermissionByUserId(userId);
//        redisTemplate.opsForValue().set(Contant.PERMISSIONS_KEY+userId,sysPermissionList,10, TimeUnit.MINUTES);
        return sysPermissionList;
    }

    @Override
// @Cacheable(value = "permissions",key = "#roleId")
    public List<SysPermission> findByRoleId(Long roleId) {

        List<SysPermission>sysPermissionList=this.baseMapper.findByRoleId(roleId);
        redisTemplate.opsForValue().set(Contant.ROLEPERMISSION_KEY+roleId,sysPermissionList,10,TimeUnit.MINUTES);
        return sysPermissionList;
    }

    @Override
    public String roleFindByUserId(Long userId) {
        return this.baseMapper.roleFindByUserId(userId);
    }
}
